﻿using System;
using System.Windows.Forms;
using BusinessObjects.Constants;
using BusinessServices;
using Core.Common;
using Core.Security;

namespace CarLeaseManageSystem
{
    public partial class FormSetting : Form
    {
        private readonly SettingService _settingService;
        private string password;

        public FormSetting()
        {
            _settingService = new SettingService();
            InitializeComponent();
        }

        private void FormCXMana_Load(object sender, EventArgs e)
        {
            var setting = _settingService.Get();

            if (setting.bRegi)
            {
                labRegiDate.Text = setting.dtRegidate.Date.ToString();
                groupBox4.Enabled = false;
            }

            chkBoxCQ.Checked = setting.bShowCQ;
            chkBoxDQ.Checked = setting.bShowDQ;
            chkBoxYD.Checked = setting.bShowYD;
            chkShowDate.Checked = setting.bShowDate;
            chkShowCapt.Checked = setting.bShowCaption;
            chkShowMsg.Checked = setting.bShowMsg;
            txtBoxCapt.Text = setting.Caption;
            password = setting.Pwd;
        }

        //保存密码更新
        private void btnOKL_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(password) && Md5.GetMd5Hash(txtOldPwd.Text) != password)
            {
                MessageBox.Show(SettingConstant.PasswordError, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtNewPwd.Text == txtRPwd.Text)
            {
                _settingService.Update(setting => setting.Pwd = Md5.GetMd5Hash(txtNewPwd.Text));
                MessageBox.Show(SettingConstant.UpdateSucceed, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(SettingConstant.PasswordDisaccord, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRegi_Click(object sender, EventArgs e)
        {
            var sProdCode = AppSettings.ProductCode;
            var getCode = string.Format("{0}-{1}-{2}-{3}", txtRegiCode1.Text, txtRegiCode2.Text, txtRegiCode3.Text, txtRegiCode4.Text);
            if (!string.Equals(getCode, sProdCode, StringComparison.OrdinalIgnoreCase))
            {
                MessageBox.Show(SettingConstant.CodeError, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                _settingService.Update(setting => setting.ProdCode = sProdCode);
                MessageBox.Show(SettingConstant.RegistSucceed, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnOKR_Click(object sender, EventArgs e)
        {
            _settingService.Update(setting =>
                                       {
                                           setting.bShowDQ = chkBoxDQ.Checked;
                                           setting.bShowCQ = chkBoxCQ.Checked;
                                           setting.bShowYD = chkBoxYD.Checked;
                                       });
            MessageBox.Show(SettingConstant.SaveChangeSucceed, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void txtRegiCode1_TextChanged(object sender, EventArgs e)
        {
            if (txtRegiCode1.TextLength == 4)
                txtRegiCode2.Focus();
        }

        private void txtRegiCode2_TextChanged(object sender, EventArgs e)
        {
            if (txtRegiCode2.TextLength == 4)
                txtRegiCode3.Focus();
        }

        private void txtRegiCode3_TextChanged(object sender, EventArgs e)
        {
            if (txtRegiCode3.TextLength == 4)
                txtRegiCode4.Focus();
        }

        private void txtRegiCode4_TextChanged(object sender, EventArgs e)
        {
            if (txtRegiCode4.TextLength == 4)
                btnRegi.Focus();
        }

        private void btnOKL2_Click(object sender, EventArgs e)
        {
            _settingService.Update(setting =>
                                       {
                                           setting.bShowMsg = chkShowMsg.Checked;
                                           setting.bShowDate = chkShowDate.Checked;
                                           setting.bShowCaption = chkShowCapt.Checked;
                                           setting.Caption = txtBoxCapt.Text;
                                       });
            MessageBox.Show(SettingConstant.SaveChangeSucceed, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void txtBoxCapt_TextChanged(object sender, EventArgs e)
        {
            if (txtBoxCapt.TextLength == 30)
            {
                txtBoxCapt.ReadOnly = true;
                btnOKL2.Focus();
            }
        }
    }
}
